//
//  WiFiScaleDataListViewController.m
//  PPScaleDemo
//
//  Created by 彭思远 on 2021/3/11.
//

#import "WiFiScaleDataListViewController.h"
#import "BodyDetailViewController.h"
#import "WiFiScaleDataListCell.h"

@interface WiFiScaleDataListViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *table;

@property (nonatomic, strong) NSArray *dataSource;
@end

@implementation WiFiScaleDataListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"WiFi scale data list";
    [self fetchData];
    
//    男，32，170cm，49.15kg。阻抗从上到下9959676，15062994，6945010，1267295
    
    
    PPUserModel *userModel2 = [[PPUserModel alloc] initWithUserHeight:162 gender:1 age:20 pregnantMode:NO athleteMode:NO andUnit:PPUnitKG];

    PPUserModel *userModel1 = [[PPUserModel alloc] initWithUserHeight:158 gender:0 age:28 pregnantMode:NO athleteMode:NO andUnit:PPUnitKG];

    
    PPBodyFatModel *model =  [[PPBodyFatModel alloc] initWithUserModel:userModel1 deviceModel:[PPDeviceModel fakeDeviceForCalcuate:PPDeviceCalcuteTypeAlternate] weight:53.65 heartRate:0 isHeartRateEnd:YES unit:PPUnitKG andImpedance:6300966];
 
    PPBodyFatModel *model1 = [[PPBodyFatModel alloc] initWithUserModel:userModel2  deviceModel:[PPDeviceModel fakeDeviceForCalcuate:PPDeviceCalcuteTypeAlternate] weight:100  heartRate:0 isHeartRateEnd:YES   unit:PPUnitKG andImpedance:6292480];
    
    PPBodyFatModel *model3 = [[PPBodyFatModel alloc] initWithUserModel:userModel2  deviceModel:[PPDeviceModel fakeDeviceForCalcuate:PPDeviceCalcuteTypeAlternate] weight:100  heartRate:0 isHeartRateEnd:YES  unit:PPUnitKG  andImpedance:6292480];
    
   
    
    
    NSLog(@"");
}

+ (instancetype)instance{
    return [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"WiFiScaleDataListViewController"];
}

- (void)fetchData{
    LFWeakSelf(self)
    NSDictionary *parameters = @{@"uid" : [IdentityManager manager].currentUser.userId};
    [[HttpRequest request] sendRequestWithHttpMethod:E_HTTP_REQUEST_METHOD_GET URLPath:@"/lefu/wifi/app/getScaleWeights" parameters: parameters completionHandler:^(id  _Nonnull data, NSError * _Nonnull error) {
        weakself.dataSource = data;
        [weakself.table reloadData];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WiFiScaleDataListCell *cell= [tableView dequeueReusableCellWithIdentifier:@"WiFiScaleDataListCell"];
    [cell fillWithDic:self.dataSource[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BodyDetailViewController *vc = [BodyDetailViewController instance];
    NSString *weightJson = self.dataSource[indexPath.row][@"weightJson"];
    NSDictionary *dic = [weightJson mj_JSONObject];
    UserModel *currentUser = [IdentityManager manager].currentUser;
    
    PPUserModel *userModel = [[PPUserModel alloc] initWithUserHeight:currentUser.userHeight gender:currentUser.userGender age:currentUser.userAge pregnantMode:currentUser.isPregnantMode athleteMode:currentUser.isAthleteMode andUnit:currentUser.userUnit];
    CGFloat weight = [dic[@"weight"] floatValue];
    NSInteger impedance = [dic[@"impedance"] integerValue];
    PPBodyFatModel *model = [[PPBodyFatModel alloc] initWithUserModel:userModel  deviceModel:[PPDeviceModel fakeDeviceForCalcuate:PPDeviceCalcuteTypeAlternate] weight:weight  heartRate:0 isHeartRateEnd:YES   unit:PPUnitKG   andImpedance:impedance];
    NSString *jsonString = [model mj_JSONString];
    vc.detailStr = [NSString stringWithFormat:@"%@", [jsonString stringByReplacingOccurrencesOfString:@"," withString:@"\n"]];;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
